<?php

use think\Db;

// 定义需要处理的文件映射
$objToFiles = [
    "item_base.json" => "data_item.php",
    "recharge_type.json" => "recharge_type.php",
    "task_1.json" => "task_1.php",
    "task_2.json" => "task_2.php",
    "task_3.json" => "task_3.php",
    "assets_data.json" => "data_assets.php",
    "hero_data.json" => "hero_data.php",
];

// 处理主要文件转换
foreach ($objToFiles as $obj => $file) {
    if (!is_file($obj)) {
        printf("跳过处理文件: %s\n", $obj);
        continue;
    }

    printf("开始处理文件: %s\n", $obj);

    $content = file_get_contents('./' . $obj);
    $data = json_decode($content, true);

    if (empty($data)) {
        printf("文件: %s 内容为空\n", $obj);
        continue;
    }

    $output = '';
    switch ($obj) {
        case "assets_data.json":
            $arrayDict = array_column($data, 'desc', "id");
            $output1 = "<?php return " . var_export($arrayDict, true) . ";";
            file_put_contents('./' . "data_economy.php", $output1);
            $output = "<?php return " . var_export($data, true) . ";";
            break;

        case 'origin.json':
            $arrayDict = array_column($data, 'desc', null);
            $output = "<?php return " . var_export($arrayDict, true) . ";";
            break;

        case 'task_3.json':
        case 'task_2.json':
        case 'task_1.json':
            // case "hero_data.json":
            $arrayList = [];
            foreach ($data as $k => $v) {
                $arrayList[(int) $v["id"]] = (string) $v["name"];
            }
            $output = "<?php return " . var_export($arrayList, true) . ";";
            break;

        default:
            $output = "<?php return " . var_export($data, true) . ";";
            break;
    }

    file_put_contents('./' . $file, $output);
}

ProcessItem();

ProcessRecharge();

function ProcessRecharge()
{
    $total = [];
    for ($i = 0; $i <= 100; $i++) {
        $f = sprintf("recharge_sub_type_%d.json", $i);
        printf("开始处理文件: %s\n", $f);
        if (!file_exists($f)) {
            echo "文件不存在" . $f . __LINE__ . PHP_EOL;
            continue;
        }

        $content = file_get_contents('./' . $f);

        $data = json_decode($content, true);
        $total = array_merge_recursive($total, $data);
        $outFile = sprintf("recharge_sub_type_%d.php", $i);
        file_put_contents($outFile, "<?php return " . var_export($data, true) . ";");
        printf("处理文件: %s完毕\n", $f);
    }
    $newlist =[];
    $newlist2 =[];
    foreach ($total as $sin) {
        $result = [];
        if (isset($sin["query_award"]) && !empty($sin["query_award"])) {
            // 方法1：使用正则表达式（更简洁）
            $result = [];
            preg_match_all('/\{(\d+),(\d+),(\d+)\}/', $sin["query_award"], $matches, PREG_SET_ORDER);

            foreach ($matches as $match) {
                $itemId = $match[1];   // 物品ID（第一个数字）
                $count = $match[3];    // 数量（第三个数字）
                $result[$itemId] = (int) $count;  // 转换为整数
            }
        }
        $sin["query_award"] = $result;
        $newlist[]= $sin;
        $newlist2["a_".$sin["type"]] = $sin["show_name"];

    }
    file_put_contents("charge_data2.php", "<?php return " . var_export($newlist2, true) . ";");

    file_put_contents("charge_data.php", "<?php return " . var_export($newlist, true) . ";");

}

function ProcessItem()
{
    printf("开始处理 item extra\n");

    $content = include("data_item.php");
    if (empty($content)) {
        return;
    }

    // 定义需要处理的字段
    $fields = [
        "code",
        "name",
        "pic",
        "type",
        "sub_type",
        "req_career",
        "req_sex",
        "req_lv",
        "req_round",
        "overlay",
        "quality",
        "desc"
    ];

    $items = [];
    $itemNames = [];
    $itemDetails = [];

    // 处理每个物品数据
    foreach ($content as $row) {
        if (!isset($row["base_id"])) {
            continue;
        }

        $row["code"] = $row["base_id"];

        // 提取需要的字段
        $item = array_combine(
            $fields,
            array_map(function ($field) use ($row) {
                return $row[$field] ?? '';
            }, $fields)
        );

        $items[] = $item;

        // 构建缓存数据
        $itemNames[$row["code"]] = $item["name"];
        $itemDetails[$row["code"]] = [
            "template_id" => $row["code"],
            "name" => $item["name"],
            "type" => $item["type"],
            "req_lv" => (int) $item["req_lv"]
        ];
    }

    // 写入缓存文件
    file_put_contents("item.php", "<?php return " . var_export($itemNames, true) . ";");
    file_put_contents("itemDetail.php", "<?php return " . var_export($itemDetails, true) . ";");
}
